package leetcode;

import org.junit.Test;

/**
 * @author: feiwang_6
 * @create: 2020/7/23 15:33
 * @description:
 */
public class 单链表反转 {
    @Test
    public void testConvert(){
        Node head = new Node(0);
        Node temp = head;
        for(int i=1; i <= 6; i++){
            temp.next = new Node(i);
            temp = temp.next;
        }
        
        temp = head;
        while(temp != null){
            System.out.print(temp.val + ",");
            temp = temp.next;
        }
        System.out.println();
        temp = convertLinkedNode(head);
        while(temp != null){
            System.out.print(temp.val + ",");
            temp = temp.next;
        }
    }

    public Node convertLinkedNode(Node old){
        Node head = new Node(-1);
        head.next = old;
        Node pre = old;
        Node pCur = old.next;
        while (pCur != null){
            pre.next = pCur.next;
            pCur.next = head.next;
            head.next = pCur;
            pCur = pre.next;
        }
        return head.next;
    }
    

    class Node{
        public Integer val;
        public Node next;

        public Node(Integer val) {
            this.val = val;
        }
    }
}
